package com.example.tossednfound;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Adds the New User into DB
 */

public class NewUserActivity extends Activity {
	Button register_button;
	EditText sqlPassword, sqlEmail, sqlAddress, sqlState, sqlZipcode,
			sqlCountry, sqlName;
	public RegUser currentUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_user);
		findViewById(R.id.found_createButton).setOnClickListener(
				new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						//register();
						//simpleRegister();
						localRegister();
					}
				});
		register_button = (Button) findViewById(R.id.found_createButton);
		sqlEmail = (EditText) findViewById(R.id.newuser_email);
		sqlPassword = (EditText) findViewById(R.id.newuser_password);
		sqlAddress = (EditText) findViewById(R.id.lost_item_address);
		sqlState = (EditText) findViewById(R.id.newuser_state);
		sqlZipcode = (EditText) findViewById(R.id.newuser_zipcode);
		sqlCountry = (EditText) findViewById(R.id.newuser_country);
		sqlName = (EditText) findViewById(R.id.newuser_name);
	}
	/**
	 * Gets all the values from the text boxes and creates a new User object, which it appends to the static list of users, then starts a new regular user home activity
	 */
	public void localRegister(){
		String email = sqlEmail.getText().toString();
		String password = sqlPassword.getText().toString();
		String address = sqlAddress.getText().toString();
		String state = sqlState.getText().toString();
		Integer zipcode = Integer.parseInt(sqlZipcode.getText().toString());
		String country = sqlCountry.getText().toString();
		String name = sqlName.getText().toString();
		currentUser = new RegUser(email, password, address, state, zipcode, country, name);
		LoginActivity.UserList.add(currentUser);
		LoginActivity.loggedIn = currentUser;
		Intent regHomeActivity = new Intent(
				"android.intent.action.REGHOMEACTIVITY");
		startActivity(regHomeActivity);
	}
	
	/**
	 * Registers New User within DB if all fields on form were inputed
	 */
	public void register() {

		// case R.id.newuser_register_button:
		String email = sqlEmail.getText().toString();
		String password = sqlPassword.getText().toString();
		String address = sqlAddress.getText().toString();
		String state = sqlState.getText().toString();
		Integer zipcode = Integer.parseInt(sqlZipcode.getText().toString());
		String country = sqlCountry.getText().toString();
		String name = sqlName.getText().toString();
		boolean successful = true;
		try {
			DatabaseHandler alreadyMade = new DatabaseHandler(
					NewUserActivity.this);
			alreadyMade.openReadable();
			try {
				if (alreadyMade.checkLogin(email, password)) {
					System.out.println("Already made an account!");
					// TODO: Add in case for Administrative Home versus Regular
					// Home
					Intent regHomeActivity = new Intent(
							"android.intent.action.REGHOMEACTIVITY");
					startActivity(regHomeActivity);
				}
			} catch (Exception e) {
				// account has not already been made
				// so continue
			}
			DatabaseHandler emailChecker = alreadyMade; // renaming for to
														// clarify what it's
														// doing
			try {
				if (!emailChecker.emailIsAvailable(email)) {
					// toast error message for email already in user
					// clear
					sqlEmail.setText(""); // clears the text
					emailChecker.close();
				}
			} catch (Exception e) { // email is available -> proceed with
									// registration
				DatabaseHandler userMaker = new DatabaseHandler(
						NewUserActivity.this);
				userMaker.openWriteable();
				try {
					userMaker.createUser(email, password, false, address,
							state, zipcode, country, name);
				} catch (Exception err) {
					// okay.. something actually went wrong here
					successful = false;
					Dialog d = new Dialog(this);
					d.setTitle("USER CREATION ERROR");
					TextView tv = new TextView(this);
					tv.setText("ERROR: " + err.getMessage());
					d.setContentView(tv);
					d.show();
				} finally {
					userMaker.close();
				}
				// create intent to go to home screen with message for user
				// successfully created
				// TODO: Add in case for Administrative Home versus Regular
				// Home
			}
		} catch (Exception e) {
			successful = false;
			Dialog d = new Dialog(this);
			d.setTitle("Heck NAW");
			TextView tv = new TextView(this);
			tv.setText("FAILURE " + e.getMessage());
			d.setContentView(tv);
			d.show();
		} finally {
			if (successful) {
				Dialog d = new Dialog(this);
				d.setTitle("Heck Yea");
				TextView tv = new TextView(this);
				tv.setText("Sucess");
				d.setContentView(tv);
				d.show();
				Intent regHomeActivity = new Intent(
						"android.intent.action.REGHOMEACTIVITY");
				startActivity(regHomeActivity);
			}
		}
		// break;
		// }

	}

	/**
	 * Registers New User within DB if only email and password in the form were inputed
	 */
	public void simpleRegister() {
		String email = sqlEmail.getText().toString();
		String password = sqlPassword.getText().toString();
		boolean successful = true;

		DatabaseHandler userMaker = new DatabaseHandler(NewUserActivity.this);
		userMaker.openWriteable();
		try {
			userMaker.simpleCreateUser(email, password);
		} catch (Exception err) {
			// okay.. something actually went wrong here
			successful = false;
			Dialog d = new Dialog(this);
			d.setTitle("USER CREATION ERROR");
			TextView tv = new TextView(this);
			tv.setText("ERROR: " + err.getMessage());
			d.setContentView(tv);
			d.show();
		} finally {
			userMaker.close();
		}
		// create intent to go to home screen with message for user
		// successfully created
		// TODO: Add in case for Administrative Home versus Regular
		// Home
		if (successful) {
			Intent regHomeActivity = new Intent(
					"android.intent.action.REGHOMEACTIVITY");
			startActivity(regHomeActivity);
		}
	}

	// break;
	// }

	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}

}
